package com.jy.adscm.operation.mask.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyCode;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.mask.bean.KzMachine;
import com.jy.adscm.operation.mask.bean.MaskEmpWork;
import com.jy.adscm.operation.mask.service.KzMachineService;
import com.jy.adscm.operation.mask.service.MaskEmpWorkService;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.system.mapper.mysql.JyCodeMapper;
import com.jy.adscm.system.service.JyUserService;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.utils.AdscmDateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * <p>
 * 口罩生产工人考勤表 前端控制器
 * </p>
 *
 * @author yyy
 * @since 2020-03-24
 */
@RestController
@RequestMapping("/adscm/operation/mask/maskEmpWork")
public class MaskEmpWorkController {


    @Autowired
    KzMachineService kzMachineService;
    @Autowired
    MaskEmpWorkService maskEmpWorkService;
    @Autowired
    JyUserService jyUserService;
    @Autowired
    JyCodeMapper jyCodeMapper;

    /**
     * 扫码开工
     * @param empId
     * @param macCode
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/startWork/{empId}/{macCode}")
    public SimpleCoreResponse startWork(@PathVariable("empId") Integer empId,@PathVariable("macCode")  String macCode) throws AdscmSystemException {

        JyUser jyUser = jyUserService.selectById(empId);
        if (jyUser == null){
            throw new AdscmSystemException("没有查询到当前的用户信息");
        }

        List<KzMachine> machines = kzMachineService.selectByCode(macCode);
        KzMachine machine = null;
        MaskEmpWork work = null;
        if (machines != null && machines.size()>0){
            machine = machines.get(0);

            Wrapper<MaskEmpWork> workWrapper = new EntityWrapper<>();
            workWrapper.eq("emp_id",empId);
            workWrapper.eq("work_mac",machine.getId());
            workWrapper.eq("belong_gx",machine.getBelongGx());
            workWrapper.eq("work_end",0);//未结束
            List<MaskEmpWork> empWorks = maskEmpWorkService.selectList(workWrapper);

            if (empWorks != null && empWorks.size()>0){
                //有未结束的工作
                work = empWorks.get(0);
            }else{
                //没有未结束，就新增
                work = new MaskEmpWork();
                work.setEmpId(empId);
                work.setWorkMac(machine.getId());
                work.setBelongGx(machine.getBelongGx());
                work.setBeginTime(new Date());
                work.setWorkEnd(0);

                maskEmpWorkService.insert(work);
            }
            List<JyCode> codeList = jyCodeMapper.getJyCodeByCodeType("MASK_GX");
            for (JyCode jyCode : codeList) {
                if (jyCode.getCode().equals(machine.getBelongGx())){
                    machine.setBelongGxDesc(jyCode.getName());
                    break;
                }
            }

            work.setEmpName(jyUser.getName());
            if (work.getBeginTime() != null){
                work.setBeginTimeDesc(AdscmDateUtils.getDateFomat(work.getBeginTime()));
            }
            if (work.getEndTime() != null){
                work.setEndTimeDesc(AdscmDateUtils.getDateFomat(work.getEndTime()));
            }


        }else {
            throw new AdscmSystemException("查询机器编码失败");
        }

        Map<String ,Object> map = new TreeMap<>();
        map.put("machine",machine);
        map.put("work",work);

        return new SimpleCoreResponse("200","成功",map);
    }

    /**
     * 结束工作
     * @param empId
     * @param macCode
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/endWork/{empId}/{macCode}")
    public SimpleCoreResponse endWork(@PathVariable("empId") Integer empId,@PathVariable("macCode")  String macCode) throws AdscmSystemException {

        JyUser jyUser = jyUserService.selectById(empId);
        if (jyUser == null){
            throw new AdscmSystemException("没有查询到当前的用户信息");
        }

        List<KzMachine> machines = kzMachineService.selectByCode(macCode);
        KzMachine machine = null;
        MaskEmpWork work = null;
        if (machines != null && machines.size()>0){
            machine = machines.get(0);

            Wrapper<MaskEmpWork> workWrapper = new EntityWrapper<>();
            workWrapper.eq("emp_id",empId);
            workWrapper.eq("work_mac",machine.getId());
            workWrapper.eq("belong_gx",machine.getBelongGx());
            workWrapper.eq("work_end",0);//未结束
            List<MaskEmpWork> empWorks = maskEmpWorkService.selectList(workWrapper);

            if (empWorks != null && empWorks.size()>0){
                //有未结束的工作
                work = empWorks.get(0);
                work.setWorkEnd(1);
                work.setEndTime(new Date());
                maskEmpWorkService.updateById(work);
            }else{
                //没有未结束
                throw new AdscmSystemException("当前工作已经结束");
            }


        }else {
            throw new AdscmSystemException("查询机器编码失败");
        }




        return new SimpleCoreResponse("200","成功",null);
    }



    /**
     * 查询工作记录，按照某个机床、某个日期
     * @param macId
     * @param date
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/selectWork/{macId}/{date}")
    public SimpleCoreResponse selectWork(@PathVariable("macId") Integer macId,@PathVariable("date")String date) throws AdscmSystemException {

        Wrapper<MaskEmpWork> workWrapper = new EntityWrapper<>();
        workWrapper.eq("work_mac",macId);
        workWrapper.ge("begin_time",date);
        workWrapper.le("begin_time",date + " 23:59:59");
        List<MaskEmpWork> works = maskEmpWorkService.selectList(workWrapper);
        if (works != null && works.size()>0){
            for (MaskEmpWork work : works) {
                Integer empId = work.getEmpId();
                JyUser jyUser = jyUserService.selectById(empId);
                if (jyUser != null){
                    work.setEmpName(jyUser.getName());
                }

                if (work.getBeginTime() != null){
                    work.setBeginTimeDesc(AdscmDateUtils.getDateFomat(work.getBeginTime()));
                }
                if (work.getEndTime() != null){
                    work.setEndTimeDesc(AdscmDateUtils.getDateFomat(work.getEndTime()));
                }else {
                    work.setEndTimeDesc("-");
                }

                if (work.getWorkEnd() != null && work.getWorkEnd().intValue() == 1){
                    work.setWorkEndDesc("已结束");
                }else {
                    work.setWorkEndDesc("未结束");
                }
            }
        }

        return new SimpleCoreResponse("200","成功",works);
    }


    /**
     * 查询某个人的未完成的工作记录集合
     * @param empId
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/selectNotEndWork/{empId}")
    public SimpleCoreResponse selectNotEndWork(@PathVariable("empId") Integer empId)throws AdscmSystemException{

        List<Map<String,String>> list = new ArrayList<>();

        JyUser jyUser = jyUserService.selectById(empId);
        if (jyUser == null){
            throw new AdscmSystemException("没有查询到用户信息");
        }

        Wrapper<MaskEmpWork> workWrapper = new EntityWrapper<>();
        workWrapper.eq("emp_id",empId);
        workWrapper.eq("work_end",0);
        List<MaskEmpWork> works = maskEmpWorkService.selectList(workWrapper);
        if (works != null && works.size()>0){
            for (MaskEmpWork work : works) {
                KzMachine machine = kzMachineService.selectById(work.getWorkMac());
                Map<String,String> map = new TreeMap<>();
                map.put("userId",jyUser.getId().toString());
                map.put("userName",jyUser.getName());
                map.put("machine",machine.getName());
                map.put("work",jyUser.getName());
                if(work.getBeginTime() != null){
                    map.put("startTime",AdscmDateUtils.getDateFomat(work.getBeginTime()));
                }
                if(work.getEndTime() != null){
                    map.put("endTime",AdscmDateUtils.getDateFomat(work.getEndTime()));
                }
                map.put("macCode",machine.getCode());
                map.put("belongGx",work.getBelongGx());
                map.put("workMac",work.getWorkMac().toString());

                list.add(map);
            }
        }
        return new SimpleCoreResponse("200","成功",list);
    }

}

